﻿//-----------------------------------------------------------------------
// <copyright file="IdentityBusinessObject.cs" company="FastNET Services">
//     Copyright (c) 2007-2013. All Rights Reserved.
// </copyright>
// <warranty>
//     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
//     BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
//     DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </warranty>
//-----------------------------------------------------------------------
#region Usings

using System;
using FastNet.BusinessSystem.Exceptions;
using FastNet.BusinessSystem.Properties;
using FastNet.RepositorySystem;
using FastNet.RepositorySystem.Interfaces;

#endregion

namespace FastNet.BusinessSystem.Objects
{

    /// <summary>
    /// Base business class for all objects with a primary key
    /// </summary>
    /// <typeparam name="TIdentityData">IIdentityData type this object will manage</typeparam>
    /// <typeparam name="TIdentityRepository">IIdentityRepository type that manages the TIdentityData</typeparam>
    public abstract class IdentityBusinessObject<TIdentityData, TIdentityRepository> : BusinessObject<TIdentityData, TIdentityRepository>
        where TIdentityData : class, IIdentityData
        where TIdentityRepository : class, IIdentityRepository<TIdentityData>
    {
        /// <summary>
        /// Initializes a new instance of the BusinessObject class
        /// </summary>
        /// <param name="factory">Factory for retrieving the Repository from</param>
        /// <param name="data">Data for backing the business object</param>
        /// <exception cref="BusinessInitializationException">Thrown if there was an error during initialization</exception>
        /// <exception cref="ArgumentNullException">Thrown if Factory or Data are null</exception>
        protected IdentityBusinessObject(IRepositoryFactory factory, TIdentityData data)
            : base(factory, data)
        {
        }

        /// <summary>
        /// Initializes a new instance of the BusinessObject class
        /// </summary>
        /// <param name="factory">Factory for retrieving the Repository from</param>
        /// <exception cref="BusinessInitializationException">Thrown if there was an error during initialization</exception>
        /// <exception cref="ArgumentNullException">Thrown if factory is null</exception>
        protected IdentityBusinessObject(IRepositoryFactory factory)
            : base(factory)
        {
        }

        /// <summary>
        /// Initializes a new instance of the BusinessObject class
        /// </summary>
        /// <param name="factory">Factory for retrieving the Repository from</param>
        /// <param name="id">ID to retrieve from the database</param>
        /// <exception cref="BusinessInitializationException">Thrown if there was an error during initialization</exception>
        /// <exception cref="ArgumentNullException">Thrown if factory is null</exception>
        /// <exception cref="BusinessRetrieveException">Thrown if the Id does not pull up a valid item</exception>
        protected IdentityBusinessObject(IRepositoryFactory factory, Identity id)
            : base(factory, false)
        {
            try
            {
                Data = Repository.Retrieve(id);
            }
            catch (Exception e)
            {
                throw new BusinessInitializationException(Resources.BusinessObject_Init_DataError, e);
            }

            if (Data == null)
                throw new BusinessRetrieveException(Resources.IdentityBusinessObject_Init_NotFound);
        }

        /// <summary>
        /// Gets the Identifier for the instance
        /// </summary>
        public Identity Id { get { return Data.Id; } }
    }
}
